import {
    md5
} from './md5'

export const Common = {
    /**
     * 对象赋值
     * @param obj1
     * @param obj12
     */
    assign: (obj1, obj2) => {
        const newObj = {}
        for (const key in obj1) {
            if (obj2.hasOwnProperty(key)) {
                newObj[key] = obj2[key]
            } else {
                newObj[key] = obj1[key]
            }
        }

        return newObj
    },

    deepClone: (obj) => {
        let objClone = Array.isArray(obj) ? [] : {};
        if (obj && typeof obj === "object") {
            for (var key in obj) {
                if (obj.hasOwnProperty(key)) {
                    //判断ojb子元素是否为对象，如果是，递归复制
                    if (obj[key] && typeof obj[key] === "object") {
                        objClone[key] = Common.deepClone(obj[key]);
                    } else {
                        //如果不是，简单复制
                        objClone[key] = obj[key];
                    }
                }
            }
        }

        return objClone;
    },

    /**
     * yyyy-M-d h:m:s
     * @param date
     * @param type
     */
    timeFormat: (date, type) => {
        if (typeof date === "string") {
            date = new Date(date)
        }

        let format = 'yyyy-M-d h:m:s'

        if (type === 1) {
            format = 'M月d日 hh:m'
        } else if (type === 2) {
            format = 'yyyy-MM-dd'
        } else if (type === 3) {
            format = 'MM-dd'
        } else if (type === 4) {
            format = 'yyyy-M-d hh:mm'
        }

        const o = {
            'M+': date.getMonth() + 1, // month
            'd+': date.getDate(), // day
            'h+': date.getHours(), // hour
            'm+': date.getMinutes(), // minute
            's+': date.getSeconds(), // second11
            'q+': Math.floor((date.getMonth() + 3) / 3), // quarter
            'S+': date.getMilliseconds() // millisecond
        }

        if (/(y+)/.test(format)) {
            format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
        }

        for (const k in o) {
            if (new RegExp('(' + k + ')').test(format)) {
                format = format.replace(
                    RegExp.$1,
                    RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
                )
            }
        }

        return format
    },

    todayPlus: (AddDayCount, formatType) => {
        var dd = new Date();
        dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的日期

        return Common.timeFormat(dd, formatType)
    },

    todayOptions: () => {
        return [{
                date: Common.todayPlus(-2, 2),
                name: '前天(' + Common.todayPlus(-2, 3) + ')'
            },
            {
                date: Common.todayPlus(-1, 2),
                name: '昨天(' + Common.todayPlus(-1, 3) + ')'
            },
            {
                date: Common.todayPlus(0, 2),
                name: '今天(' + Common.todayPlus(0, 3) + ')'
            },
            {
                date: Common.todayPlus(1, 2),
                name: '明天(' + Common.todayPlus(1, 3) + ')'
            },
            {
                date: Common.todayPlus(2, 2),
                name: '后天(' + Common.todayPlus(2, 3) + ')'
            },
            {
                date: Common.todayPlus(3, 2),
                name: '大后天(' + Common.todayPlus(3, 3) + ')'
            }
        ]
    },

    timeOptions: () => {
        return ['00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00',
            '09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00',
            '19:00', '20:00',
            '21:00', '22:00', '23:00'
        ];
    },

    jsonSort: (json, key) => {
        for (let j = 1; j < json.length; j++) {
            const temp = json[j]
            const val = temp[key]
            let i = j - 1
            while (i >= 0 && json[i][key] > val) {
                json[i + 1] = json[i]
                i = i - 1
            }
            json[i + 1] = temp
        }
        return json
    },

    /**
     * 获取签名
     * @param data 签名数据
     * @param param 额外参数
     */
    getSign: (data, param) => {
        const signData = getSignData(data)

        const urlStr = signData.concat(param).sort().join('&')

        // 最后⼀个是转成⼤写,根据实际规则确定是否需要转
        const sign = md5(urlStr).toUpperCase()

        return sign
    },

    getSignData: (param) => {
        let arr = []
        if (typeof param === 'object' && param.length == undefined) {
            for (const key in param) {
                if (typeof param[key] === 'object') {
                    if (param[key][0] != 'object') {
                        const val = JSON.stringify(param[key])
                        if (val != '[]') {
                            arr.push(key + '=' + JSON.stringify(param[key]))
                        }
                    } else {
                        arr = arr.concat(getSignData(param[key]))
                    }
                } else if ((param[key] != '' && param[key] != undefined && param[key] != null) || typeof param[
                        key] === 'boolean') {
                    arr.push(key + '=' + param[key])
                }
            }
        } else {
            param.forEach(function(item) {
                arr = arr.concat(getSignData(item))
            })
        }

        return arr
    },
    getParentNode(arrData, data, nodeId, arrRes) {
        if (!data) {
            if (!nodeId) {
                arrRes.unshift(nodeId);
            }
            return arrRes;
        }
        for (let i = 0, length = data.length; i < length; i++) {
            let node = data[i];
            if (node.id === nodeId) {
                arrRes.unshift(nodeId);
                if (node.parentId > 0) {
                    this.getParentNode(arrData, arrData, node.parentId, arrRes);
                }
                break;
            } else {
                if (!!node.children) {
                    this.getParentNode(arrData, node.children, nodeId, arrRes);
                }
            }

        }
        return arrRes;
    }
}
